﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class ExportInvoiceDAL
    {
        private ManageStoreDataContext db;

        public ExportInvoiceDAL()
        {
            db = new ManageStoreDataContext();
        }

        public List<ExportInvoice> ExportInvoiceGetAll()
        {
            var Exp = from exp in db.ExportInvoices
                      orderby exp.ExportDate
                      select exp;

            return Exp.ToList<ExportInvoice>();
        }

        public ExportInvoice ExportInvoiceGetByID(int ExpID)
        {
            ExportInvoice Exps = db.ExportInvoices.SingleOrDefault<ExportInvoice>(exp => exp.ExportID == ExpID);

            return Exps;
        }

        public bool ExportInvoiceInsert(ExportInvoice Exps)
        {
            try
            {
                db.ExportInvoices.InsertOnSubmit(Exps);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool ExportInvoiceDeleteByID(int ExpID)
        {
            ExportInvoice Exps = db.ExportInvoices.Single<ExportInvoice>(exp => exp.ExportID == ExpID);

            if (Exps == null)
                return false;
            db.ExportInvoices.DeleteOnSubmit(Exps);

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool ExportInvoiceUpdate(ExportInvoice Exps)
        {
            ExportInvoice Exp = db.ExportInvoices.Single<ExportInvoice>(exp => exp.ExportID == Exps.ExportID);

            if (Exp == null)
                return false;

            Exp.InvoceID = Exps.InvoceID;
            Exp.StorageID = Exps.StorageID;
            Exp.ExportDate = Exps.ExportDate;
            Exp.CustomerID = Exps.CustomerID;
            Exp.EmployeeID = Exps.EmployeeID;
            Exp.TotalValues = Exps.TotalValues;

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool ExportInvoiceCheckDulicateInvoiceID(string InvoiceID)
        {
            var Emps = from e in db.ExportInvoices
                       select e;
            if (Emps.ToList<ExportInvoice>().Count == 0)
                return true;
            else
            {
                ExportInvoice Exp = db.ExportInvoices.Single<ExportInvoice>(exp => exp.InvoceID.Equals(InvoiceID));

                if (Exp == null)
                    return true;
                return false;
            }
        }

        public List<ExportInvoices_GetProductByExportIDandStorageIDResult>
            ExportInvoices_GetProductByExportIDandStorageID(int ExportID, int StorageID)
        {
            var ExportPros = db.ExportInvoices_GetProductByExportIDandStorageID(ExportID, StorageID);

            if (ExportPros == null)
                return null;
            else
                return ExportPros.ToList<ExportInvoices_GetProductByExportIDandStorageIDResult>();
        }

        public ExportInvoice ExportInvoiceGetByInvoiceID(string invoiceid)
        {
            ExportInvoice ExpInvoice = db.ExportInvoices.SingleOrDefault<ExportInvoice>(exp => exp.InvoceID.Equals(invoiceid));

            if (ExpInvoice == null)
                return null;
            else
                return ExpInvoice;
        }
    }
}
